package com.f6.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

public class HttpUtil {
	// 超时时间
	private static final int TIME_OUT = 60000;
	
	// 请求方式
	private static final String METHOD = "POST";

	// 类型
	private static final String CONTENT_TYPE = "Content-Type";

	private static final String CONTENT_TYPE_VALUE = "application/x-www-form-urlencoded";

	// 长度
	private static final String CONTENT_LENGTH = "Content-Length";
	
	/** 
     * 发起http请求获取返回结果 
     *  
     * @param requestUrl 请求地址 
     * @return 
     */  
    public static String httpRequest(String requestUrl) {  
        StringBuffer buffer = new StringBuffer();  
        try {  
            URL url = new URL(requestUrl);  
            HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();  
  
            httpUrlConn.setDoOutput(false);  
            httpUrlConn.setDoInput(true);  
            httpUrlConn.setUseCaches(false);  
  
            httpUrlConn.setRequestMethod("GET");  
            httpUrlConn.connect();  
  
            // 将返回的输入流转换成字符串  
            InputStream inputStream = httpUrlConn.getInputStream();  
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");  
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
  
            String str = null;  
            while ((str = bufferedReader.readLine()) != null) {  
                buffer.append(str);  
            }  
            bufferedReader.close();  
            inputStreamReader.close();  
            // 释放资源  
            inputStream.close();  
            inputStream = null;  
            httpUrlConn.disconnect();  
  
        } catch (Exception e) {  
        }  
        return buffer.toString();  
    }  
    
    public static String getJsonSring(String rquestJsonStr, String requestUrl)
	{
		OutputStream outputStream = null;
		BufferedReader bufferedReader = null;
		try 
		{
			URL Url = new URL(requestUrl);
			HttpURLConnection HttpConn = (HttpURLConnection) Url
					.openConnection();
			HttpConn.setRequestMethod(METHOD);
			HttpConn.setReadTimeout(TIME_OUT);
			HttpConn.setDoOutput(true);
			HttpConn.setRequestProperty(CONTENT_TYPE, CONTENT_TYPE_VALUE);
			HttpConn.setRequestProperty(CONTENT_LENGTH,
					String.valueOf(rquestJsonStr.getBytes().length));
			outputStream = HttpConn.getOutputStream();
			outputStream.write(rquestJsonStr.getBytes());

			if (HttpURLConnection.HTTP_OK == HttpConn.getResponseCode())
			{
				String result = "";
				String line = null;
				bufferedReader = new BufferedReader(new InputStreamReader(
						HttpConn.getInputStream()));
				while (null != (line = bufferedReader.readLine()))
				{
					result += line;
				}

				bufferedReader.close();
				return result;
			}
		} 
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		} 
		catch (ProtocolException e)
		{
			e.printStackTrace();
		} 
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally 
		{
			if (null != outputStream)
			{
				try 
				{
					outputStream.close();
				} 
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
			
			if (null != bufferedReader)
			{
				try
				{
					bufferedReader.close();
				} 
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}

		return null;
	}
    
    /** 
     * utf编码 
     *  
     * @param source 
     * @return 
     */  
    public static String urlEncodeUTF8(String source) {  
        String result = source;  
        try {  
            result = java.net.URLEncoder.encode(source, "utf-8");  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  
        return result;  
    } 
}
